package com.wzq.controller;

import com.wzq.dao.pojo.Code;
import com.wzq.dao.pojo.Result;
import com.wzq.dao.pojo.Type;
import com.wzq.service.ITypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yzc
 * @since 2022-08-31
 */
@RestController
@RequestMapping("/type")
public class TypeController {

    @Autowired
    private ITypeService iTypeService;
    //   添加
    @PostMapping
    public Result save(@RequestBody Type type) {
        boolean flag = iTypeService.save(type);
        return new Result(flag ? Code.SAVE_OK:Code.SAVE_ERR,flag);
    }
    //   修改
    @PutMapping
    public Result update(@RequestBody Type type) {
        boolean flag = iTypeService.updateById(type);
        return new Result(flag ? Code.UPDATE_OK:Code.UPDATE_ERR,flag);
    }

    //    id删除
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        boolean flag = iTypeService.removeById(id);
        return new Result(flag ? Code.DELETE_OK:Code.DELETE_ERR,flag);
    }

    //    获得id对应用户信息
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Type type = iTypeService.getById(id);
        Integer code = type != null ? Code.GET_OK : Code.GET_ERR;
        String msg = type != null ? "" : "数据查询失败，请重试！";
        return new Result(code,type,msg);
    }

    @GetMapping("/onetitle/{onetitle}")
    public Result getByOneTitle(@PathVariable int onetitle) {
        List<Integer> ids = iTypeService.getBytitle(onetitle);
        Integer code = ids != null ? Code.GET_OK : Code.GET_ERR;
        String msg = ids != null ? "" : "数据查询失败，请重试！";
        return new Result(code,ids,msg);
    }
    //     获得所有用户信息
    @GetMapping
    public Result getAll() {
        List<Type> list = iTypeService.list();
        System.out.println(list);
        Integer code = list != null ? Code.GET_OK : Code.GET_ERR;
        String msg = list != null ? "" : "数据查询失败，请重试！";
        return new Result(code,list,msg);
    }

    //  展示活动分类
    @GetMapping("getActivityType")
    public Result getActivityType(){
        List<Type> typeList = iTypeService.getActivityType();
        return new Result(Code.GET_OK,typeList,"查询成功");
    }

    //  展示资源分类
    @GetMapping("getResourcesType")
    public Result getResourcesType(){
        List<Type> typeList = iTypeService.getResourcesType();
        return new Result(Code.GET_OK,typeList,"查询成功");
    }

    //展示资源分类中的搜索
    @GetMapping("getResourcesTypeSearch")
    public Result getResourcesTypeSearch(@RequestParam("title") String title){
        List<Type> typeList = iTypeService.getResourcesTypeSearch(title);
        return new Result(typeList!= null ? Code.GET_OK: Code.GET_ERR,typeList);
    }

    //展示活动分类中的搜索
    @GetMapping("getActivityTypeSearch")
    public Result getActivityTypeSearch(@RequestParam("title") String title){
        List<Type> typeList = iTypeService.getActivityTypeSearch(title);
        return new Result(typeList!= null ? Code.GET_OK: Code.GET_ERR,typeList);
    }

//    //利用P_ID判断该项目是活动还是资源
//    @GetMapping("getparent")
//    public Result getparent(@RequestParam("typeid") String typeid){
//        List<Type> typeList = iTypeService.getById(typeid)
//    }

}
